package com.cn.utils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CalAspect {

    @Pointcut("execution(* com.cn.bean.*.*(..))")
    private void myPointCut() {
    }

    @Before("myPointCut()")
    public void before(JoinPoint point) {
        System.out.println(point.getSignature().getName() + "方法开始执行了！");
    }

    @AfterReturning(value = "myPointCut()", returning = "result")
    public void afterrun(JoinPoint point, Object result) {
        System.out.println(point.getSignature().getName() + "方法执行结束，结果为：" + result);
    }

    @Around(value = "myPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕开始：执行目标方法前，模拟开启事务！");
        Object proceed = point.proceed();
        System.out.println("环绕结束：执行目标方法后，模拟关闭事务！");
        return proceed;
    }

    @AfterThrowing(value = "myPointCut()", throwing = "exception")
    public void throwthing(JoinPoint point, Exception exception) {
        System.out.println(point.getSignature().getName() + "方法出现异常了，异常原因为" + exception);
    }

    @After(value = "myPointCut()")
    public void after(){
        System.out.println("最终通知，释放资源");
    }


}
